package com.moduls.interfaceFunction.controller;
import com.moduls.interfaceFunction.core.Result;
import com.moduls.interfaceFunction.core.ResultGenerator;
import com.moduls.interfaceFunction.model.TTest;
import com.moduls.interfaceFunction.service.TTestService;
import com.moduls.interfaceFunction.core.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
* Created by MiniGenerator on 2018-05-06T16:12:55.500.
*/
@RestController
@RequestMapping("/api/ttest")
public class TTestController {
    @Autowired
    private TTestService tTestService;

    @PostMapping("/add")
    public Result add(@RequestBody TTest tTest) {
        ValidationUtils.validate(tTest);
        tTestService.addEntity(tTest);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam String id) {
        tTestService.deleteEntityById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody TTest tTest) {
        ValidationUtils.validate(tTest);
        tTestService.updateEntity(tTest);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/getall")
    public Result getAll() {
        List<TTest> tTests = tTestService.getAll();
        return ResultGenerator.genSuccessResult(tTests);
    }

    @PostMapping("/getid")
    public Result getEntityById(@RequestParam String id) {
        TTest tTest = tTestService.getEntityById(id);
        return ResultGenerator.genSuccessResult(tTest);
    }

    @PostMapping("/getpagelist")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<TTest> tTests = tTestService.getAll();
        PageInfo pageInfo = new PageInfo(tTests);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @PostMapping("/getallwhere")
    public Result getAllWhere(@RequestParam String wheresql) {
        List<TTest> tTests = tTestService.getAllWhere(wheresql);
        return ResultGenerator.genSuccessResult(tTests);
    }

    @PostMapping("/batchinsert")
    public Result batchInsert(@RequestBody List<TTest> tTests ) {
        int inserts= tTestService.batchInsert(tTests );
        return ResultGenerator.genSuccessResult(inserts);
        }

}
